/**
 * Project Name:HomeActivity
 * Date:2016-3-29下午9:17:22
 * Copyright (c) 2016, askzhao All Rights Reserved.
 *
 */
package com.kkd.kkdapp.view;

import java.text.DecimalFormat;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.ObjectAnimator;
import android.animation.TimeInterpolator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.TextView;

/**
 * ClassName: CountViewFloat <br/>
 * date: 2016-3-29 下午9:17:22 <br/>
 *
 * @author askzhao
 * @version 
 */
public class CountViewFloat extends TextView {

	private DecimalFormat mDf;//"#,###.00"
    float number;
    
    public CountViewFloat(Context context) {
        super(context);
    }

    public CountViewFloat(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    ObjectAnimator objectAnimator;
    public void showNumberWithAnimation(float from, final float add, int duration) {
    	try {
    		if(objectAnimator!=null){
    			objectAnimator.cancel();
    			objectAnimator = null;
    		}
    		// 修改number属性，会调用setNumber方法
    		objectAnimator = ObjectAnimator.ofFloat(this, "number", from, from + add);
            objectAnimator.setDuration(duration);
            // 加速器，从慢到快到再到慢
            objectAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
            objectAnimator.start();
            objectAnimator.addListener(new AnimatorListener() {
				
				@Override
				public void onAnimationStart(Animator animation) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onAnimationRepeat(Animator animation) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onAnimationEnd(Animator animation) {
					// TODO Auto-generated method stub
					if(mDf!=null)
			        	setText(mDf.format(add));
			        else
			        	setText(String.valueOf(add));
				}
				
				@Override
				public void onAnimationCancel(Animator animation) {
					// TODO Auto-generated method stub
					
				}
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
        
    }
    
    public void showNumberWithAnimation(float from, final String add, int duration) {
    	float to =  (float) Double.parseDouble(add.replaceAll(",", ""));
        // 修改number属性，会调用setNumber方法
    	if(objectAnimator!=null){
			objectAnimator.cancel();
			objectAnimator = null;
		}
        objectAnimator = ObjectAnimator.ofFloat(this, "number", (float)from, (float)(from + to));
        objectAnimator.setDuration(duration);
        // 加速器，从慢到快到再到慢
        objectAnimator.setInterpolator(new TimeInterpolator() {
			
			@Override
			public float getInterpolation(float input) {
				// TODO Auto-generated method stub
				return (float)(Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;
			}
		});
        objectAnimator.addListener(new AnimatorListener() {
			
			@Override
			public void onAnimationStart(Animator animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationRepeat(Animator animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animator animation) {
				// TODO Auto-generated method stub
				setText(add);
			}
			
			@Override
			public void onAnimationCancel(Animator animation) {
				// TODO Auto-generated method stub
				
			}
		});
        objectAnimator.start();
    }

    public float getNumber() {
        return number;
    }

    public void setNumber(float num) {
        number = num;
        if(mDf!=null)
        	setText(mDf.format(number));
        else
        	setText(String.valueOf(number));
    }
    public void setNumber(String num) {
    	
        number = (float) Double.parseDouble(num);
        if(mDf!=null)
        	setText(mDf.format(number));
        else
        	setText(String.valueOf(number));
    }
    public void setDecimalFormat(DecimalFormat df){
    	mDf = df;
    }

	/**
	 * stopAnimatio:(这里用一句话描述这个方法的作用). <br/>
	 * @author askzhao
	 */
	public void stopAnimatio() {
		if(objectAnimator!=null){
			objectAnimator.cancel();
			objectAnimator = null;
		}
		
	}
}
